<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>使用查询结构修改相关度 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-scoring.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-scoring.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/query-scoring.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/query-scoring.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="controlling-relevance.html">控制相关度</a></span>
»
<span class="breadcrumb-node">使用查询结构修改相关度</span>
</div>
<div class="navheader">
<span class="prev">
<a href="query-time-boosting.html">« 查询时权重提升</a>
</span>
<span class="next">
<a href="not-quite-not.html">Not Quite Not »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-scoring"></a>使用查询结构修改相关度<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/25_Query_scoring.asciidoc">edit</a>
</h2>
</div></div></div>
<p>Elasticsearch 的查询表达式相当灵活，可以通过调整查询结构中查询语句的所处层次，从而或多或少改变其重要性，比如，设想下面这个查询：</p>
<pre class="literallayout">quick OR brown OR red OR fox</pre>

<p>可以将所有词都放在 <code class="literal">bool</code> 查询的同一层中：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "term": { "text": "quick" }},
        { "term": { "text": "brown" }},
        { "term": { "text": "red"   }},
        { "term": { "text": "fox"   }}
      ]
    }
  }
}</pre>
</div>
<p>这个查询可能最终给包含 <code class="literal">quick</code> 、 <code class="literal">red</code> 和 <code class="literal">brown</code> 的文档评分与包含 <code class="literal">quick</code> 、 <code class="literal">red</code> 、 <code class="literal">fox</code> 文档的评分相同，这里 <em>Red</em> 和 <em>brown</em> 是同义词，可能只需要保留其中一个，而我们真正要表达的意思是想做以下查询：</p>
<pre class="literallayout">quick OR (brown OR red) OR fox</pre>

<p>根据标准的布尔逻辑，这与原始的查询是完全一样的，但是我们已经在 <a class="xref" href="bool-query.html" title="组合查询">组合查询（Combining Queries）</a> 中看到， <code class="literal">bool</code> 查询不关心文档匹配的 <em>程度</em> ，只关心是否能匹配。</p>
<p>上述查询有个更好的方式：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "term": { "text": "quick" }},
        { "term": { "text": "fox"   }},
        {
          "bool": {
            "should": [
              { "term": { "text": "brown" }},
              { "term": { "text": "red"   }}
            ]
          }
        }
      ]
    }
  }
}</pre>
</div>
<p>现在， <code class="literal">red</code> 和 <code class="literal">brown</code> 处于相互竞争的层次， <code class="literal">quick</code> 、 <code class="literal">fox</code> 以及 <code class="literal">red OR brown</code> 则是处于顶层且相互竞争的词。</p>
<p>我们已经讨论过如何使用 <a class="xref" href="match-query.html" title="匹配查询"><code class="literal">match</code></a> 、<a class="xref" href="multi-match-query.html" title="multi_match 查询"><code class="literal">multi_match</code></a> 、<a class="xref" href="term-vs-full-text.html" title="基于词项与基于全文"><code class="literal">term</code></a> 、<a class="xref" href="bool-query.html" title="组合查询"><code class="literal">bool</code></a> 和 <a class="xref" href="_best_fields.html#dis-max-query" title="dis_max 查询"><code class="literal">dis_max</code></a> 查询修改相关度评分。本章后面的内容会介绍另外三个与相关度评分有关的查询： <code class="literal">boosting</code> 查询、 <code class="literal">constant_score</code> 查询和 <code class="literal">function_score</code> 查询。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="query-time-boosting.html">« 查询时权重提升</a>
</span>
<span class="next">
<a href="not-quite-not.html">Not Quite Not »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>